Spot-On
spot-on-sctp-socket.h
1 /*
2 ** Copyright (c) 2011 - 10^10^10, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Spot-On without specific prior written permission.
15 **
16 ** SPOT-ON IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** SPOT-ON, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef _spoton_sctp_socket_h_
29 #define _spoton_sctp_socket_h_
30 
31 #include <QAbstractSocket>
32 #include <QHostInfo>
33 #include <QObject>
34 #include <QTimer>
35 
36 class spoton_sctp_socket: public QObject
37 {
38  Q_OBJECT
39 
40  public:
41  enum NetworkLayerProtocol
42  {
43  IPv4Protocol = QAbstractSocket::IPv4Protocol,
44  IPv6Protocol = QAbstractSocket::IPv6Protocol
45  };
46 
47  enum SocketError
48  {
49  ConnectionRefusedError = QAbstractSocket::ConnectionRefusedError,
50  HostNotFoundError = QAbstractSocket::HostNotFoundError,
51  NetworkError = QAbstractSocket::NetworkError,
52  RemoteHostClosedError = QAbstractSocket::RemoteHostClosedError,
53  SocketAccessError = QAbstractSocket::SocketAccessError,
54  SocketResourceError = QAbstractSocket::SocketResourceError,
55  SocketTimeoutError = QAbstractSocket::SocketTimeoutError,
56  UnfinishedSocketOperationError =
57  QAbstractSocket::UnfinishedSocketOperationError,
58  UnknownSocketError = QAbstractSocket::UnknownSocketError,
59  UnsupportedSocketOperationError =
60  QAbstractSocket::UnsupportedSocketOperationError
61  };
62 
63  enum SocketOption
64  {
65  KeepAliveOption = QAbstractSocket::KeepAliveOption,
66  LowDelayOption = QAbstractSocket::LowDelayOption
67  };
68 
69  enum SocketState
70  {
71  ConnectedState = QAbstractSocket::ConnectedState,
72  ConnectingState = QAbstractSocket::ConnectingState,
73  HostLookupState = QAbstractSocket::HostLookupState,
74  UnconnectedState = QAbstractSocket::UnconnectedState
75  };
76 
77  spoton_sctp_socket(QObject *parent);
79  QByteArray readAll(void);
80  QHostAddress localAddress(void) const;
81  QHostAddress peerAddress(void) const;
82  QString peerName(void) const;
83  SocketState state(void) const;
84  bool setSocketDescriptor(const int socketDescriptor);
85  int socketDescriptor(void) const;
86  qint64 write(const char *data, const qint64 maxSize);
87  quint16 localPort(void) const;
88  quint16 peerPort(void) const;
89  void abort(void);
90  void close(void);
91  void connectToHost(const QString &hostName,
92  const quint16 port,
93  const QString &socketOptions);
94  void setReadBufferSize(const qint64 size);
95  void setSocketOption(const SocketOption option, const QVariant &value);
96 
97  private:
98  QByteArray m_readBuffer;
99  QString m_connectToPeerName;
100  QString m_ipAddress;
101  QString m_socketOptions;
102  QTimer m_timer;
103  SocketState m_state;
104  int m_hostLookupId;
105 #if defined(Q_OS_WIN)
106  SOCKET m_socketDescriptor;
107 #else
108  int m_socketDescriptor;
109 #endif
110  qint64 m_readBufferSize;
111  quint16 m_connectToPeerPort;
112  QHostAddress localAddressAndPort(quint16 *port) const;
113  QHostAddress peerAddressAndPort(quint16 *port) const;
114  int inspectConnectResult(const int rc, const int errorcode);
115  int setSocketBlockingOrNon(void);
116  qint64 read(char *data, const qint64 maxSize);
117  void connectToHostImplementation(void);
118 
119  private slots:
120  void slotHostFound(const QHostInfo &hostInfo);
121  void slotTimeout(void);
122 
123  signals:
124  void connected(void);
125  void disconnected(void);
126  void error(const QString &method,
127  const spoton_sctp_socket::SocketError socketError);
128  void readyRead(void);
129 };
130 
131 #endif
Definition: spot-on-sctp-socket.h:36